package com.uiao.stone.controller.login;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "login")
public class LoginController {

    @GetMapping("prelogin")
    public String preLogin(HttpServletRequest request) {
        return "login";
    }

    @RequestMapping("/dologin")
    public @ResponseBody
    Map<String, Object> doLogin(HttpServletRequest request, String username, String password) {
        Map<String, Object> modelMap = new HashMap();
        if (username != null) {
            if (username.equals("cuihao") && password.equals("123")) {

                HttpSession session = request.getSession();
                session.setAttribute("username", username);
                System.out.println("***************用户登入成功***************");
                modelMap.put("status", 0);

            } else {
                System.out.println("***************用户密码错误***************");
                modelMap.put("status", 1);

            }
        }
        return modelMap;
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        System.out.println("***************用户已登出***************");
        return "redirect:/";
    }
}
